package com.example.vue_shop.components.rights.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.vue_shop.components.rights.entity.Rights;
import com.example.vue_shop.components.rights.entity.TreeRights;
import com.example.vue_shop.components.rights.mapper.RightsMapper;
import com.example.vue_shop.components.rights.service.RightsService;
import com.example.vue_shop.components.rights.utils.RightsUtils;
import com.example.vue_shop.utils.TreeUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author lipengwei
 */
@Service
public class RightsServiceImpl implements RightsService {

    @Resource(name = "rightsMapper")
    private RightsMapper rightsMapper;

    @Override
    public Map<String, Object> getRightsList() {
        List<Rights> rightsList = rightsMapper.selectList(new QueryWrapper<>());
        List<TreeRights> treeRights = RightsUtils.getTreeRights(rightsList);
        Map<String, Object> resultMap = new HashMap<>(1);
        resultMap.put("treeRights", treeRights);
        return resultMap;
    }

    @Override
    public List<Rights> getRightsList(int roleId) {
        return rightsMapper.getRightsListByRoleId(roleId);
    }

    @Override
    public Map<String, Object> getAllRightsTreePage(int pageNum, int pageSize) {
        // 查询条件对象
        QueryWrapper<Rights> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("rights_grade", "1");
        // 查询的分页对象
        Page<Map<String, Object>> page = new Page<>();
        page.setCurrent(pageNum);
        page.setSize(pageSize);
        // 查询到的分页结果
        Page<Map<String, Object>> pageMapRights =
                rightsMapper.selectMapsPage(page, queryWrapper);
        List<Map<String, Object>> rightsMapList = pageMapRights.getRecords();
        // 查询所有的权限对象
        List<Map<String, Object>> rightsAllMapList = rightsMapper.getRightsAll();
        List<Map<String, Object>> pageTree =
                TreeUtils.getPageTree(rightsMapList, rightsAllMapList, "id",
                        "parentId");
        Map<String, Object> resultMap = new HashMap<>(2);
        resultMap.put("total", pageMapRights.getTotal());
        resultMap.put("treeRights", pageTree);
        return resultMap;
    }

    @Override
    public List<TreeRights> getAllRightsTree() {
        List<Rights> rights = rightsMapper.selectList(new QueryWrapper<>());
        return RightsUtils.getTreeRights(rights);
    }

    @Override
    public List<Rights> getAllRightsList() {
        return rightsMapper.selectList(new QueryWrapper<>());
    }

    @Override
    public void addRights(Rights rights) {
        // 等级
        String rightsGrade = rights.getRightsGrade();
        // 获取父节点id
        int parentId = rights.getParentId();
        QueryWrapper<Rights> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id", parentId);
        Rights parentRights = rightsMapper.selectOne(queryWrapper);
        // 获取父节点的编号
        String parentRightsCode = parentRights.getCode();
        // 查询有父节点有几个子对象
        queryWrapper.clear();
        queryWrapper.eq("parent_id", parentId);
        List<Rights> rightsList = rightsMapper.selectList(queryWrapper);
        int size = rightsList.size();

        String nextCode = getNextCode(parentRightsCode, rightsGrade, size);
        rights.setCode(nextCode);
        rightsMapper.insert(rights);
    }

    @Override
    public void updateRights(Rights rights) {
        rightsMapper.updateById(rights);
    }

    @Override
    public void delRights(int id) {
        rightsMapper.deleteById(id);
    }


    /**
     * 获取下一个权限编码.
     *
     * @param parentRightsCode 父节点编码
     * @param rightsGrade      节点等级
     * @param size             当前节点数量
     * @return String
     */
    private String getNextCode(String parentRightsCode, String rightsGrade,
                               int size) {

        String sizeStr = "";
        int isAddZero = 9;
        if (size + 1 < isAddZero) {
            sizeStr = "0" + (size + 1);
        }

        // 1级， 直接补4个0
        // 2级，截取前两位+00，
        // 3级 截取前四位
        String nextCode;
        switch (rightsGrade) {
            case "2":
                String substring02 = parentRightsCode.substring(0, 2);
                nextCode = substring02 + sizeStr + "00";
                break;
            case "3":
                String substring04 = parentRightsCode.substring(0, 4);
                nextCode = substring04 + sizeStr;
                break;
            case "1":
            default:
                nextCode = sizeStr + "0000";
                break;
        }
        return nextCode;
    }

}
